package com.cheer.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.dao.MallProductBookStoreDao;
import com.cheer.entity.MallProductBookStoreEntity;
import com.cheer.entity.MallProductEntity;
import com.cheer.exception.ServiceException;
import com.cheer.listener.ProductListener;
import com.cheer.param.MallProductBookStoreParam;
import com.cheer.service.MallCategoryProductRelationService;
import com.cheer.service.MallProductBookStoreService;
import com.cheer.service.MallProductService;
import com.cheer.vo.MallProductBookStoreVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.hutool.core.bean.BeanUtil.copyProperties;


@Service("mallProductBookStoreService")
public class MallProductBookStoreServiceImpl extends ServiceImpl<MallProductBookStoreDao, MallProductBookStoreEntity> implements MallProductBookStoreService {

    @Autowired
    private MallProductService mallProductService;

    @Autowired
    private ProductListener productListener;

    @Autowired
    private MallCategoryProductRelationService mallCategoryProductRelationService;


    @Override
    public MallProductBookStoreVO info(Long id) {

        List<MallProductBookStoreEntity> list = list(new QueryWrapper<MallProductBookStoreEntity>()
                .lambda().eq(MallProductBookStoreEntity::getProductId, id));
        if(CollectionUtil.isNotEmpty(list)){
            MallProductBookStoreEntity mallProductBookStoreEntity = list.get(0);
            MallProductBookStoreVO convert = Convert.convert(MallProductBookStoreVO.class, mallProductBookStoreEntity);
            return convert;
        }else{
            return null;
        }
    }

    public void check(Long productId){
        MallProductEntity mallProductEntity = mallProductService.getById(productId);
        if(mallProductEntity == null){
            throw new ServiceException("未查询到产品记录");
        }
        if( mallProductEntity.getStock() != 0 ){
            throw new ServiceException("产品库存不为0时不能预定产品！");
        }
    }

    @Override
    public void update(MallProductBookStoreParam mallProductBookStoreParam) {

        Long id = mallProductBookStoreParam.getId();
        check(mallProductBookStoreParam.getProductId());
        MallProductBookStoreEntity mallProductBook = getById(id);
        if(ObjectUtil.isNull(mallProductBook)){
            throw new ServiceException("未查询到预定记录");
        }
        copyProperties(mallProductBookStoreParam, mallProductBook);
        updateById(mallProductBook);
        mallCategoryProductRelationService.updateCountDownSecondByProductId(mallProductBookStoreParam.getProductId(), mallProductBookStoreParam.getBookDate().getTime()/1000);
        mallCategoryProductRelationService.resetCategorySort(mallProductBookStoreParam.getProductId());
        productListener.addQueue(mallProductBook);
    }


    @Override
    public void add(MallProductBookStoreParam mallProductBookStoreParam) {
        // 查询当前是否存在预定记录
        Long productId = mallProductBookStoreParam.getProductId();
        check(productId);
        MallProductBookStoreVO info = info(productId);
        if(info == null){
            mallProductBookStoreParam.setId(null);
            MallProductBookStoreEntity convert = Convert.convert(MallProductBookStoreEntity.class, mallProductBookStoreParam);
            save(convert);
            mallProductService.updateIsBookStock(productId, 1);
            // 产品分类置顶
            mallCategoryProductRelationService.updateCountDownSecondByProductId(productId, mallProductBookStoreParam.getBookDate().getTime()/1000);
            mallCategoryProductRelationService.resetCategorySort(productId);
            productListener.addQueue(convert);
        }else{
            mallProductBookStoreParam.setId(info.getId());
            update(mallProductBookStoreParam);
        }
    }

    @Override
    public void delete(Long id) {

        MallProductBookStoreEntity mallProductBookStoreEntity = getById(id);
        removeById(id);
        Long productId = mallProductBookStoreEntity.getProductId();
        mallProductService.updateIsBookStock(productId, 0);
        mallCategoryProductRelationService.updateCountDownSecondByProductId(productId, null);
        mallCategoryProductRelationService.resetCategorySort(productId);
    }



}